<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../../vue.js"></script>
  </head>
  <body>
    <div id="app">
      <table width="300" border="1" style="border-collapse: collapse">
        <tr>
          <th>是否选中</th>
          <th>商品名称</th>
          <th>备注</th>
        </tr>
        <tr v-for="item in shopCar" ::key="item.id">
          <th><input type="checkbox" v-model="item.isChecked" /></th>
          <th>{{item.name}}</th>
          <th></th>
        </tr>
      </table>
      <div><input type="checkbox" v-model="allCheck" />全选</div>
    </div>
    <script>
      Vue.config.productionTip = false;
      const vm = new Vue({
        el: "#app",
        data() {
          return {
            shopCar: [
              { id: 1, name: "素力高猫粮", isChecked: false },
              { id: 2, name: "抗原试纸", isChecked: false },
              { id: 3, name: "布洛芬缓释胶囊", isChecked: false },
            ],
          };
        },
        computed: {
          /* 
                计算属性其实完整的写法是可读可写，我们如果是只读的话，我们只需要写成函数即可
                如果这个计算属性可读可写，则我们需要写成一个对象，对象用set和get函数，分别对应着写和读的函数
                set函数接受的参数就是我们设置的值
            */
          allCheck: {
            get() {
              return this.shopCar.every((item) => item.isChecked);
            },
            set(value) {
              //console.log(value);
              this.shopCar.forEach((item) => {
                item.isChecked = value;
              });
            },
          },
        },
      });
    </script>
  </body>
</html>
